package com.zjmzxfzhl.modules.app.controller;

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import com.zjmzxfzhl.common.core.util.SecurityUtils;
import com.zjmzxfzhl.modules.app.form.AppDocVo;
import com.zjmzxfzhl.modules.app.form.AppModiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.base.BaseController;
import com.zjmzxfzhl.modules.app.entity.AppModi;
import com.zjmzxfzhl.modules.app.service.AppModiService;

/**
 * Controller
 * 
 * @author 张森荣
 */
@RestController
@RequestMapping("/app/modi")
public class AppModiController extends BaseController {
    @Autowired
    private AppModiService appModiService;

    /**
     * 自定义查询列表
     * 
     * @param appModiVo
     * @param current
     * @param size
     * @return
     */
//    @PreAuthorize("@elp.single('app:modi:list')")
    @GetMapping(value = "/list")
    public Result list(AppModiVo appModiVo, @RequestParam Integer current, @RequestParam Integer size) {
        IPage<AppModiVo> pageList = appModiService.list(new Page<AppModiVo>(current, size), appModiVo);
        return Result.ok(pageList);
    }

//    @PreAuthorize("@elp.single('app:modi:list')")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        AppModi appModi = appModiService.getById(id);
        return Result.ok(appModi);
    }

    /**
     * @功能：新增
     * @param appModi
     * @return
     */
    @PreAuthorize("@elp.single('app:modi:save')")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody AppModi appModi) {
        appModiService.save(appModi);
        return Result.ok();
    }

    /**
     * @功能：修改
     * @param appModi
     * @return
     */
    @PreAuthorize("@elp.single('app:modi:update')")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody AppModi appModi) {
        appModiService.updateById(appModi);
        return Result.ok();
    }

    /**
     * @功能：批量删除
     * @param ids
     * @return
     */
//    @PreAuthorize("@elp.single('app:modi:delete')")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        //查看我参与项目的任务
        String userId = SecurityUtils.getUserId();
        if(!"admin".equals(userId)){
            return Result.error("变更记录仅超级管理员可删！");
        }
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        Boolean result=appModiService.delByIds(ids);
        return Result.ok();
    }

    /**
     * @功能：发起变更（00.项目,01.任务,02.产品,04.文档,05.合同）
     * @param appModi
     * @return
     */
//    @PreAuthorize("@elp.single('app:modi:save')")
    @PostMapping(value = "/launchChange")
    public Result launchChange(@Valid @RequestBody AppModi appModi) {
        if(appModi.getModiType()==null||appModi.getModiTypeId()==null){
            return Result.error("变更类型和变更类型ID不可为空！");
        }
        Boolean bool =true;
        switch (appModi.getModiType()) {
            case "00": //项目变更
                switch (appModi.getModiSubtype()) {
                    case "00": //普通变更
                    case "01": //中止变更
                    case "02": //重启变更
                    case "03": //终止变更
                        bool = appModiService.projChange(appModi.getModiTypeId(),appModi.getModiSubtype());
                        break;
                    default: return Result.error("查找项目变更子类型失败！");
                }
                break;
            case "01": //任务变更

                break;
            case "02": //产品变更

                break;
            case "03": //文档变更

                break;
            case "04": //合同变更

                break;
            default: return Result.error("查找变更类型失败！");
        }
        if(bool==false){
            return Result.error("当前处在无需变更状态或无法变更状态！");
        }
        AppModi result=appModiService.launchChange(appModi);
        return Result.ok(result);
    }

    @GetMapping(value = "/procModi")
    public Result procModi(@RequestParam String processInstanceId) {
        List<AppModiVo> appModiList =appModiService.procModi(processInstanceId);
        AppModi result=null;
        if(appModiList.size()>0){
            result=appModiList.get(0);
        }
        return Result.ok(result);
    }
}
